﻿<%@ Control Language="C#" AutoEventWireup="true" Inherits="Portal.Data.Webparts.WebpartControl" %>
<%@ Import Namespace="Portal.Core.Security" %>
<%@ Import Namespace="Portal.Data.Entity" %>
<link rel="stylesheet" href="/jRating/jRating.jquery.css" type="text/css" />
<div style="padding: 10px;">
    <h1 class="tit_cate">
        <asp:Literal ID="ltrHeaderInfo" runat="server"></asp:Literal>
    </h1>
    <div class="gioi_thieu">
        <div class="share">
            <ul class="share-box">
                <li class="fb-like-wrapper">
                    <div class="fb-like" data-href="<%=BookUrlAbsolute %>" data-width="135" data-layout="button_count" data-action="like" data-show-faces="true" data-share="true"></div>
                </li>
                <li class="g-plusone-wrapper">
                    <div class="g-plusone" data-size="medium" data-href="<%=BookUrlAbsolute %>"></div>
                </li>
            </ul>
        </div>
        <div class="ttin_truyen">
            <div id="rating">
                <div style="text-align: center;">
                    <div class="rate-point"><span><%=RateAverage %></span></div>
                </div>
                <div id="rate-bar" data-average="<%=RateAverage %>" data-id="<%=BookId %>"></div>
                <div style="margin-top: 5px; text-align: center;">
                    <div class="rate-total">
                        <i class="reviewers-small"></i>
                        <span><%=RateTotal %></span> lượt đánh giá
                    </div>
                </div>
            </div>
            <div style="width: 100%;">
                <asp:Literal runat="server" ID="ltrImage"></asp:Literal>
                <ul>
                    <asp:Literal ID="ltrInfo" runat="server"></asp:Literal>
                    <li class="btn">
                        <a href="<%=FirstChapUrl %>">
                            <span class="arrow">Đọc từ đầu</span>
                        </a>
                    </li>
                    <li class="btn">
                        <a href="<%=LastChapUrl %>">
                            <span class="arrow">Đọc chap mới nhất</span>
                        </a>
                    </li>
                </ul>
                <div class="nd_gt clear_fix">
                    <hr class="filigree" />
                    <asp:Literal ID="ltrDescription" runat="server"></asp:Literal>
                </div>
            </div>
            <!--END danh_gia-->
        </div>
        <!--END ttin_truyen-->
        <div class="clear" style="height: 20px;"></div>
        <div class="tim_trang" style="width: auto">
            <ul>
                <li>
                    <label>Đến chap:</label>
                    <input id="txtChapter" type="text" class="den_muc" />
                    <input id="btnViewChap" type="button" value=" Go " />
                </li>
            </ul>
        </div>
        <!--END tim_trang-->
        <!--END nd_gt-->
        <div class="ds_chuong clear_fix">
            <table>
                <tr>
                    <th>Chap</th>
                    <th>Tên chap</th>
                    <th>Ngày cập nhật</th>
                </tr>
                <asp:Repeater ID="Items" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td nowrap><a href="<%#Eval("Url")%>">--<%#Eval("Chapter")%>--</a></td>
                            <td class="ten_chuong"><a href="<%#Eval("Url")%>"><%#Eval("Title")%></a></td>
                            <td nowrap><%#Portal.Core.Common.FriendlyDateDisplay(Eval("CreatedDate"))%></td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
        </div>
        <!--END ds_chuong-->
    </div>
    <!--END gioi_thieu-->
</div>
<div class="clear_fix"></div>

<%if (!Portal.Core.Utils.MyCheck.IsMobileClient()){%>
<div id="fb-block">
    <div class="fb-comments" data-href="<%=BookUrlAbsolute%>" data-width="650" data-num-posts="5"></div>
</div>
<%} %>
<script type="text/javascript" src="/jRating/jRating.jquery.js"></script>
<script type="text/javascript">
    var supportStorage = (typeof (Storage) !== "undefined");
    jQuery(function () {
        var bid, keyRateTotal, keyRateAverage, rateTotal, rateAverage;
        bid = '<%=BookId %>';
        rateTotal = $('.rate-total span');
        rateAverage = $('.rate-point span');
        keyRateTotal = "bookRateTotal:" + bid;
        keyRateAverage = "bookRateAverage:" + bid;

        //init after loaded page
        var v1 = parseInt(localStorage.getItem(keyRateTotal)) || 0;
        var v2 = parseInt('<%=RateTotal %>') || 0;
        rateTotal.html(v1 > v2 ? v1 : v2);
        
        var v3 = parseFloat(localStorage.getItem(keyRateAverage)) || 0;
        var v4 = parseFloat('<%=RateAverage %>') || 0;
        rateAverage.html(v3 > v4 ? v3 : v4);

        $('#rate-bar').jRating({
            rateMax: 10,
            //step:true,
            //showRateInfo:false,
            onSuccess: function (data) {
                if (supportStorage) {
                    localStorage.setItem(keyRateTotal, data.total);
                    localStorage.setItem(keyRateAverage, data.average);
                }
                rateTotal.html(data.total);
                rateAverage.html(data.average);
            },
            onError: function (data) {
                console.log(data);
            }
        });

        $('#txtChapter').keyup(function (e) {
            var key = e.keyCode || e.which;
            if (key === 13) {
                goChap(bid, $(this).val());
            }
        });
        $('#btnViewChap').click(function () {
            goChap(bid, $('#txtChapter').val());
        });
        bookIncr(bid, 0);
    });
</script>
<script runat="server">    
    public string BookUrlAbsolute, ImageThumb, Title, Description, CategoryUrl, CategoryName, FirstChapUrl, LastChapUrl;
    public int BookId, RateTotal;
    public double RateAverage;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BookId = Convert.ToInt32(Common.GetViewId());
            if (BookId == 0) throw new HttpNotFoundException();
            var book = Book.FindByPrimaryKey(BookId);
            if (book == null || !book.Status) throw new HttpNotFoundException();

            if (!book.Url.Equals(Request.RawUrl, StringComparison.OrdinalIgnoreCase))
            {
                Response.StatusCode = 301;
                Response.AddHeader("Location", book.Url);
                return;
            }
            ltrHeaderInfo.Text = book.FullName;

            RateAverage = book.RateAverage;
            RateTotal = book.RateTotal;
            Page.Title = book.FullName;
            var author = Author.FindByPrimaryKey(book.AuthorId);
            BookUrlAbsolute = Portal.Core.Common.GetAbsoluteUrl(book.Url);
            var first = Portal.Data.Entity.Content.FindFirst(BookId);
            FirstChapUrl = (first != null) ? first.URL : "#";
            var last = Portal.Data.Entity.Content.FindLatest(BookId);
            LastChapUrl = (last != null) ? last.URL : "#";

            var name = (!string.IsNullOrEmpty(book.OtherName)) ? book.OtherName : book.FullName;
            ltrInfo.Text += string.Format("<li>Tên khác: <strong>{0}</strong></li>", name);
            ltrInfo.Text += string.Format("<li>Luợt xem: <strong>{0}</strong></li>", book.TotalVisit);
            ltrInfo.Text += string.Format("<li>Tổng chap: <strong>{0}</strong></li>", book.LastChapter);
            ltrInfo.Text += string.Format("<li>Tình trạng: <strong>{0}</strong></li>", book.FinishDisplay);
            ltrInfo.Text += string.Format("<li>Cập nhật: <strong>{0}</strong></li>", Portal.Core.Common.FriendlyDateDisplay(book.ModifiedDate));

            if (author != null)
                ltrInfo.Text += string.Format("<li>Tác giả: <a href='{0}'>{1}</a></li>", author.Url, author.DisplayFullName);
            else
                ltrInfo.Text += string.Format("<li>Tác giả: {0}</li>", string.IsNullOrEmpty(book.CrawlAuthor) ? "không rõ" : book.CrawlAuthor);

            if (!string.IsNullOrEmpty(book.Source))
                ltrInfo.Text += string.Format("<li>Nguồn: <strong>{0}</strong></li>", book.Source);

            var buildListType = book.HistoryTypes();
            if (buildListType.Length == 0) buildListType = book.Types;
            ltrInfo.Text += string.Format("<li>Thể loại: <strong>{0}</strong></li>", buildListType);

            if (!string.IsNullOrEmpty(book.Image))
            {
                ImageThumb = Book.RenderImage(book.Image, 300);
                ltrImage.Text = string.Format("<img class='cover' src='{0}' alt=\"{1}\"/>", ImageThumb, book.FullName);
            }
            if (!string.IsNullOrEmpty(book.Description))
                ltrDescription.Text = string.Format("<p>{0}</p>", book.Description);

            var contents = Portal.Data.Entity.Content.FindByBook(book.ID).OrderByDescending(x => x.Chapter);
            Items.DataSource = contents;
            Items.DataBind();

            //meta
            var metatag = new LiteralControl();
            metatag.Text = string.Format("<meta name='description' content='{0}'/>", book.SeoDescription());
            metatag.Text += string.Format("<meta name='keywords' content='{0}'/>", book.SeoKeywords());
            Page.Header.Controls.Add(metatag);
        }
    }    

</script>
